package com.nelson.musicplayer.business.home.adapter

import com.alibaba.android.vlayout.LayoutHelper
import com.alibaba.android.vlayout.layout.SingleLayoutHelper
import com.chad.library.adapter.base.BaseViewHolder
import com.nelson.musicplayer.R
import com.nelson.musicplayer.base.BaseDelegateAdapter
import com.nelson.musicplayer.helper.GlideImageLoader
import com.nelson.musicplayer.source.bean.HomeData
import com.youth.banner.Banner
import com.youth.banner.BannerConfig
import java.lang.Exception

/**
 * @author nelson
 * @since 2020-07-29
 */
class BannerAdapter(itemViewType: Int) : BaseDelegateAdapter(R.layout.home_banner_item, itemViewType) {

    private val mLayoutHelper: SingleLayoutHelper = SingleLayoutHelper()

    private val mData: ArrayList<HomeData.Banner> = ArrayList()
    private val mImages: ArrayList<String> = ArrayList()

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        try {
            val banner = holder.itemView as Banner
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
            banner.setImageLoader(GlideImageLoader())
            banner.isAutoPlay(true)
            banner.setImages(mImages)
            banner.setDelayTime(5000)
            banner.setIndicatorGravity(BannerConfig.CENTER)
            banner.start()
        } catch (e: Exception) {

        }
    }

    fun setData(data: List<HomeData.Banner>) {
        mData.addAll(data)
        mImages.addAll(buildImages(data))
        notifyDataSetChanged()
    }

    private fun buildImages(data: List<HomeData.Banner>): ArrayList<String> {
        val images = ArrayList<String>();
        data.forEach {
            images.add(it.imgurl)
        }
        return images
    }

    override fun getItemCount(): Int {
        if (mData == null) {
            return 0
        } else {
            return 1
        }
    }

    override fun onCreateLayoutHelper(): LayoutHelper = mLayoutHelper
}